// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie;

import java.util.ArrayList;
import zombie.characters.IsoPlayer;
import zombie.characters.IsoZombie;
import zombie.iso.IsoMovingObject;
import zombie.iso.IsoWorld;
import zombie.network.GameServer;
import zombie.vehicles.BaseVehicle;

public final class MovingObjectUpdateScheduler {
    public static final zombie.MovingObjectUpdateScheduler instance = new zombie.MovingObjectUpdateScheduler();
    final zombie.MovingObjectUpdateSchedulerUpdateBucket fullSimulation = new zombie.MovingObjectUpdateSchedulerUpdateBucket(1);
    final zombie.MovingObjectUpdateSchedulerUpdateBucket halfSimulation = new zombie.MovingObjectUpdateSchedulerUpdateBucket(2);
    final zombie.MovingObjectUpdateSchedulerUpdateBucket quarterSimulation = new zombie.MovingObjectUpdateSchedulerUpdateBucket(4);
    final zombie.MovingObjectUpdateSchedulerUpdateBucket eighthSimulation = new zombie.MovingObjectUpdateSchedulerUpdateBucket(8);
    final zombie.MovingObjectUpdateSchedulerUpdateBucket sixteenthSimulation = new zombie.MovingObjectUpdateSchedulerUpdateBucket(16);
    long frameCounter;
    private boolean isEnabled = true;

    public long getFrameCounter() {
        return this.frameCounter;
    }

    public void startFrame() {
        this.frameCounter++;
        this.fullSimulation.clear();
        this.halfSimulation.clear();
        this.quarterSimulation.clear();
        this.eighthSimulation.clear();
        this.sixteenthSimulation.clear();
        ArrayList arrayList = IsoWorld.instance.getCell().getObjectList();

        for (int int0 = 0; int0 < arrayList.size(); int0++) {
            IsoMovingObject isoMovingObject = (IsoMovingObject)arrayList.get(int0);
            if (!GameServer.bServer || !(isoMovingObject instanceof IsoZombie)) {
                boolean boolean0 = false;
                boolean boolean1 = false;
                float float0 = 1.0E8F;
                boolean boolean2 = false;

                for (int int1 = 0; int1 < IsoPlayer.numPlayers; int1++) {
                    IsoPlayer isoPlayer = IsoPlayer.players[int1];
                    if (isoPlayer != null) {
                        if (isoMovingObject.getCurrentSquare() == null) {
                            isoMovingObject.setCurrent(
                                IsoWorld.instance.getCell().getGridSquare((double)isoMovingObject.x, (double)isoMovingObject.y, (double)isoMovingObject.z)
                            );
                        }

                        if (isoPlayer == isoMovingObject) {
                            boolean2 = true;
                        }

                        if (isoMovingObject.getCurrentSquare() != null) {
                            if (isoMovingObject.getCurrentSquare().isCouldSee(int1)) {
                                boolean0 = true;
                            }

                            if (isoMovingObject.getCurrentSquare().isCanSee(int1)) {
                                boolean1 = true;
                            }

                            float float1 = isoMovingObject.DistTo(isoPlayer);
                            if (float1 < float0) {
                                float0 = float1;
                            }
                        }
                    }
                }

                int int2 = 3;
                if (!boolean1) {
                    int2--;
                }

                if (!boolean0 && float0 > 10.0F) {
                    int2--;
                }

                if (float0 > 30.0F) {
                    int2--;
                }

                if (float0 > 60.0F) {
                    int2--;
                }

                if (float0 > 80.0F) {
                    int2--;
                }

                if (isoMovingObject instanceof IsoPlayer) {
                    int2 = 3;
                }

                if (isoMovingObject instanceof BaseVehicle) {
                    int2 = 3;
                }

                if (GameServer.bServer) {
                    int2 = 3;
                }

                if (boolean2) {
                    int2 = 3;
                }

                if (!this.isEnabled) {
                    int2 = 3;
                }

                if (int2 == 3) {
                    this.fullSimulation.add(isoMovingObject);
                }

                if (int2 == 2) {
                    this.halfSimulation.add(isoMovingObject);
                }

                if (int2 == 1) {
                    this.quarterSimulation.add(isoMovingObject);
                }

                if (int2 == 0) {
                    this.eighthSimulation.add(isoMovingObject);
                }

                if (int2 < 0) {
                    this.sixteenthSimulation.add(isoMovingObject);
                }
            }
        }
    }

    public void update() {
        zombie.GameTime.getInstance().PerObjectMultiplier = 1.0F;
        this.fullSimulation.update((int)this.frameCounter);
        this.halfSimulation.update((int)this.frameCounter);
        this.quarterSimulation.update((int)this.frameCounter);
        this.eighthSimulation.update((int)this.frameCounter);
        this.sixteenthSimulation.update((int)this.frameCounter);
    }

    public void postupdate() {
        zombie.GameTime.getInstance().PerObjectMultiplier = 1.0F;
        this.fullSimulation.postupdate((int)this.frameCounter);
        this.halfSimulation.postupdate((int)this.frameCounter);
        this.quarterSimulation.postupdate((int)this.frameCounter);
        this.eighthSimulation.postupdate((int)this.frameCounter);
        this.sixteenthSimulation.postupdate((int)this.frameCounter);
    }

    public boolean isEnabled() {
        return this.isEnabled;
    }

    public void setEnabled(boolean enabled) {
        this.isEnabled = enabled;
    }

    public void removeObject(IsoMovingObject object) {
        this.fullSimulation.removeObject(object);
        this.halfSimulation.removeObject(object);
        this.quarterSimulation.removeObject(object);
        this.eighthSimulation.removeObject(object);
        this.sixteenthSimulation.removeObject(object);
    }

    public ArrayList<IsoMovingObject> getBucket() {
        return this.fullSimulation.getBucket((int)this.frameCounter);
    }
}
